package www.webols;

import www.webols.utils.StringUtil;
import android.app.Activity;
import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class LoginActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		WebView weiboPage = (WebView) findViewById(R.id.weibopage);
		weiboPage.getSettings().setJavaScriptEnabled(true);
		weiboPage.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				// TODO Auto-generated method stub
				super.onProgressChanged(view, newProgress);
			}
		});
		weiboPage.setWebViewClient(new WebViewClient() {
			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, SslError error) {
				handler.proceed();
			};

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Log.d("url", url);
				if (StringUtil.isCallBackUrl(url)) {
					String code=StringUtil.getWeiboCode(url);
					Intent intent=new Intent();
					intent.putExtra("code", code);
				    Activity activity=(Activity)view.getContext();
				    activity.setResult(2, intent);
				    activity.finish();
					return false;
				}
				return super.shouldOverrideUrlLoading(view, url);
			}
		});
		weiboPage
				.loadUrl(StringUtil.WEIBO_OAUTH_URL);
		
		Log.d("on_create", "loginActivity created.");
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d("on_start", "loginActivity started");
	}
}
